
import cv2
import numpy as np
import imageio
import matplotlib.pyplot as plt
import time
from skimage import measure
from skimage import transform
from PIL import Image
import os


data_dir = "H:/data/meshb/meshb_processed/val/"



def fusion_show(img1,imh2):
    image1 = imageio.imread(img1)
    image2 = imageio.imread(imh2)
    inds = np.where(image2[:,:,[1]] > 0)
    image2[inds] = 255
    # image2[:,:,[1]] %= 255
    overlapping = cv2.addWeighted(image1, 0.7, image2, 0.3, 0)
    # filename = os.path.basename(img_maskfile1)
    # Image.fromarray(overlapping).save(img_maskfile2+filename)
    return overlapping

    #overlapping = cv2.addWeighted(image1_mask, 0.5, imgOut, 0.5, 0)
    # plt.rcParams['savefig.dpi'] = 100 #图片像素
    # #plt.rcParams['figure.dpi'] = 100 #分辨率
    # #plt.rcParams['figure.figsize'] = (16.0, 9.0) # 设置figure_size尺寸
    # plt.imshow(overlapping)
    # # filename = os.path.basename(img_maskfile1)
    # # plt.savefig(img_maskfile2+filename)

    # plt.show()

def test():
    fanme = "@7.091556969883849@477.6362882005051@335.8500511161983@-89.9@-59.9@0.0@113.532608456@34.816178958@335.868@.png"
    file1 = data_dir + "images/" + fanme
    file2 = data_dir + "masks/" + fanme
    fusion_show(file1,file2)

def main():
    #目录路径
    image_dir = data_dir + "images/"
    mask_dir = data_dir + "masks/"
    fusion_dir = data_dir + "fusion/"



    for file in os.listdir(image_dir):
        ovimg = fusion_show(image_dir+"/" + file,mask_dir+"/" + file)
        Image.fromarray(ovimg).save(fusion_dir+"/"+file)
        

main()
